package com.pingtai.dao;

import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.pingtai.indent.entity.UserIndent;
import com.pingtai.util.PaginationSupport;

public class AbstractManager  {

    private SessionFactory sessionFactory;
    private Session session;
	
	public Session getSession(){
		if(session==null||!session.isOpen()){
			return sessionFactory.getCurrentSession();
		}
		return session;
	}
    
    
    
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

   
  
    public PaginationSupport findPageByCriteria(final Criteria Criteria) {  
        return findPageByCriteria(Criteria, PaginationSupport.PAGESIZE, 0);  
    }  
  
    public PaginationSupport findPageByCriteria(final Criteria criteria, final int startIndex) {  
        return findPageByCriteria(criteria, PaginationSupport.PAGESIZE, startIndex);  
    }  
  
    public PaginationSupport findPageByCriteria(final Criteria criteria, final int pageSize,  
            final int startIndex) { 
    	try {
    		long totalCount = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue();
        	criteria.setProjection(null);
        	List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list(); 
        	
        	PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex); 
        	
//        	boolean flag = false;
//        	
//        	if(items!=null&&items.size()>0){
//        		Object obj = items.get(0);
//        		if(obj instanceof UserIndent){
//        			flag = true;
//        		}
//        	}
//        	
//        	if(flag){
//        		ProjectionList projectionList = Projections.projectionList();
//            	projectionList.add(Projections.groupProperty("state"));
//            	projectionList.add(Projections.rowCount());
//            	criteria.setProjection(projectionList);
//            	List indents = criteria.list();
//            	Iterator iterator = indents.iterator();
//            	while(iterator.hasNext()) {
//            	    Object[] o = (Object[]) iterator.next();
//            	    if(o[0].equals(0)){
//            	    	ps.setShenheCount((Long)o[1]);
//            	    }else if(o[0].equals(1)){
//            	    	ps.setZhixingCount((Long)o[1]);
//            	    }else if(o[0].equals(2)){
//            	    	ps.setWanchengCount((Long)o[1]);
//            	    }else if(o[0].equals(3)){
//            	    	ps.setTuikuanCount((Long)o[1]);
//            	    }
//            	}
//        	}
        	
        	 return ps; 
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
    	return null;
    	
    }  
  
    public List findAllByCriteria(final Criteria criteria) {  
        return criteria.list();
    }  
  
    public int getCountByCriteria(final Criteria criteria) {  
    	return ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
    } 
    
    
}
